package com.zhentao.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.annotation.Log;
import com.zhentao.entity.product.Brand;
import com.zhentao.menus.OperatorType;
import com.zhentao.service.BrandService;
import com.zhentao.vo.common.Result;
import com.zhentao.vo.common.ResultCodeEnum;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value="/admin/product/brand")
public class BrandController {

    @Resource
    private BrandService brandService ;

    /**
     * 品牌列表
     * @param page
     * @param limit
     * @return
     */
    @Log(title = "品牌列表",businessType = 0,operatorType = OperatorType.MANAGE)
    @GetMapping("/{page}/{limit}")
    public Result<Page<Brand>> findByPage(@PathVariable Integer page, @PathVariable Integer limit) {
        Page<Brand> pageInfo = brandService.findByPage(page, limit);
        return Result.build(pageInfo , ResultCodeEnum.SUCCESS) ;
    }

    /**
     * 添加品牌
     * @param brand
     * @return
     */
    @PostMapping("save")
    public Result save(@RequestBody Brand brand) {
        Brand brand1 = new Brand();
        brand1.setName(brand.getName());
        brand1.setLogo(brand.getLogo());
        brand1.setCreateTime(new Date());
        brand1.setUpdateTime(new Date());
        brand1.setIsDeleted(0);
        brandService.save(brand1);
        return Result.build(null , ResultCodeEnum.SUCCESS);
    }

    /**
     * 修改品牌
     * @param brand
     * @return
     */
    @PutMapping("updateById")
    public Result updateById(@RequestBody Brand brand) {
        brand.setUpdateTime(new Date());
        brandService.updateById(brand);
        return Result.build(null , ResultCodeEnum.SUCCESS);
    }
    /**
     * 删除品牌
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Long id) {
        brandService.removeById(id);
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    /**
     * 品牌下拉列表
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll() {
        List<Brand> list = brandService.findAll();
        return Result.build(list , ResultCodeEnum.SUCCESS) ;
    }
}